package until;

import io.restassured.RestAssured;
import io.restassured.response.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static config.commonconfig.*;
import static io.restassured.RestAssured.given;

/**
 * @Description
 * @auther lileilei
 * @create 2020-03-01 18:06
 */

/*
获取远程stf设备封装。
1.获取所有设备
2.设备信息
3.申请设备
4.归还设备
5.获取设备远程测试地址
 */
public class StfDevicesOpearting {

    public List<String> getModelt() {
        //申请可用设备根据设备的列表
        List<String> mobilelist = new ArrayList<>();
        RestAssured.baseURI = stfurl;
        Response response = given()
                .contentType("application/json;charset=UTF-8")
                .header("Authorization", "Bearer " + stftoken)
                .request()
                .get("/api/v1/devices");
        StringJsonParson stringJsonParson = new StringJsonParson();
        List<Map<String, String>> reslut = (List<Map<String, String>>) stringJsonParson.json2map(response.asString()).get("devices");
        for (Map<String, String> map : reslut) {
            if (String.valueOf(map.get("using")).equals("true") && map.get("platform").toString().equals(testModelType)) {
                mobilelist.add(map.get("serial"));
            }
        }
        if (mobilelist.size() < testModelNum) {
            return null;
        }
        return mobilelist.subList(0, testModelNum);
    }

    public Map<String, String> getOneMobileInfo(String name) {
        /*
        获取设备信息
         */
        Map<String, String> mobileinfo = new HashMap<>();
        RestAssured.baseURI = stfurl;
        Response response = given()
                .contentType("application/json;charset=UTF-8")
                .header("Authorization", "Bearer " + stftoken)
                .request()
                .get("/api/v1/devices/" + name);
        StringJsonParson stringJsonParson = new StringJsonParson();
        Map<String, String> reslut = (Map<String, String>) stringJsonParson.json2map(response.asString()).get("device");

        mobileinfo.put("version", reslut.get("version"));
        mobileinfo.put("platform", reslut.get("platform"));
        mobileinfo.put("name", reslut.get("serial"));
        return mobileinfo;
    }

    public boolean getoneMobileUse(List<String> userMobilelist) {
        /*
        申请设备
         */
        for (String name : userMobilelist) {
            RestAssured.baseURI = stfurl;
            Response response = given()
                    .contentType("application/json;charset=UTF-8")
                    .header("Authorization", "Bearer " + stftoken)
                    .param("serial", name)
                    .request()
                    .post("/api/v1/user/devices");
        }
        return true;
    }

    public boolean returnMobileUse(List<String> userMobilelist) {
        /*
        归还设备
         */
        for (String name : userMobilelist) {
            RestAssured.baseURI = stfurl;
            Response response = given()
                    .contentType("application/json;charset=UTF-8")
                    .header("Authorization", "Bearer " + stftoken)
                    .request()
                    .delete("/api/v1/user/devices/" + name);
        }
        return true;
    }

    public String getmobileName(String mobilename) {
         /*
        获取设备链接地址
         */
        RestAssured.baseURI = stfurl;
        Response response = given()
                .contentType("application/json;charset=UTF-8")
                .header("Authorization", "Bearer " + stftoken)
                .request()
                .delete("/api/v1/devices/" + mobilename);
        StringJsonParson stringJsonParson = new StringJsonParson();
        Map<String, String> reslut = (Map<String, String>) stringJsonParson.json2map(response.asString()).get("device");

        return reslut.get("remoteConnectUrl").toString();
    }


}
